home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Gizmo Project 4.0.4 / WinGizmoInstall.exe / apbarSp.Gizmo.exe / {pf} / Mozilla Firefox / components / AskSearch.js
Text File  |  2008-07-15  |  11KB  |  366 lines

  1.  
  2. // You can change these if you like
  3. const CLASS_ID = Components.ID("26caa3d0-139e-4d18-acf4-3b759886880c");
  4. const CLASS_NAME = "Ask Search";
  5. const CONTRACT_ID = "@ask/AskSearch;1";
  6.  
  7.  
  8. var DNS = null;
  9. var isListenerSet = null;
  10. var ffwindow = null;
  11.  
  12. function get_DNSService() 
  13. {
  14.         dump("\n\nget_DNSService function called\n\n");
  15.         if( !DNS ) 
  16.         {
  17.                 DNS = Components.classes['@mozilla.org/network/dns-service;1'].getService(Components.interfaces.nsIDNSService);
  18.         }
  19.         dump("\n\nfunction:get_DNSService Completed " + DNS + "\n\n");
  20.         return DNS;
  21. }
  22.  
  23. var listener =
  24. {
  25.       QueryInterface : function(aIID)
  26.       /* {00000000-0000-0000-c000-000000000046} */
  27.       {
  28.               dump("\n\nDNSlistener: QueryInterface(" + aIID + ")\n\n");
  29.               if (aIID.equals(Components.interfaces.nsIDNSListener) ||
  30.                   aIID.equals(Components.interfaces.nsISupports) ) 
  31.               {
  32.                   dump("\n\nDNSlistener: QueryInterface : Successful\n\n");
  33.                   return this;
  34.               }
  35.               throw Components.results.NS_NOINTERFACE;
  36.               Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
  37.               return null;
  38.       },
  39.       
  40.       onLookupComplete: function( aRequest, aRecord, aStatus )
  41.       {
  42.             dump("\n\nDNSistener: onLookupComplete(" + aRequest + ", " + aRecord + ", " + aStatus +")\n\n");
  43.             if( this._thumb && aStatus ) 
  44.             {
  45.                   var sIp = aRecord.getNextAddrAsString();
  46.                   if( sIp ) 
  47.                   {
  48.                     dump("\n\nDNSlistener: onLookupComplete() sIp = " + sIp +"\n\n" );
  49.                   }
  50.             }    
  51.       }
  52. }
  53.  
  54.  
  55. function sResolve(domain) 
  56. {
  57.     dump("\n\nsResolve(" + domain + ")\n\n");
  58.         
  59.         var dns = get_DNSService();
  60.         if( dns ) 
  61.         {
  62.                 dump("\n\nDNS Service : " + dns + "\n\n");
  63.                 var ip = null;
  64.                 if( domain ) 
  65.                 {
  66.                     dump("\n\nDomain : " + domain + "\n\n");
  67.                     try
  68.                     {
  69.                     ip = dns.resolve( domain, false).getNextAddrAsString();
  70.                         }
  71.                         catch( ex )
  72.                         {
  73.                     dump("\n\nsResolve() Exception : " + domain + ":"+ex +"\n\n");
  74.                     return false;
  75.                         }
  76.                         if( ip ) 
  77.                         {
  78.                                 dump("\n\nsResolve() Resolving '" + domain + "' as '" + ip +"\n\n");
  79.                         }
  80.                 }
  81.         }
  82.         dump("\n\nsResolve() return\n\n");
  83.         return true;
  84. }
  85.  
  86. function aResolve(domain) 
  87. {
  88.         dump("\n\naResolve(" + domain + ")\n\n");
  89.         var dns = get_DNSService();
  90.         if( dns ) 
  91.         {
  92.                 var ip = null;
  93.                 if( domain ) 
  94.                 {
  95.                         try 
  96.                         {
  97.                                 ip = dns.asyncResolve( domain, false, listener, null );
  98.                         }
  99.                         catch( ex ) 
  100.                         {
  101.                                 dump("\n\naResolve: Exception : " + href +"\n\n");
  102.                         }
  103.                 }
  104.         }
  105.         dump("\n\naResolve() return\n\n");
  106.         return false;
  107. }
  108.  
  109.  
  110. var HomePageForeverListener =
  111. {
  112.     QueryInterface: function(aIID)
  113.           {
  114.             if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
  115.                   aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
  116.                   aIID.equals(Components.interfaces.nsISupports ))
  117.                   {
  118.                       return this;
  119.                   }
  120.               throw Components.results.NS_NOINTERFACE;
  121.           },
  122.  
  123.  
  124.     onStateChange:function(aProgress, aRequest, aFlag, aStatus)
  125.           {
  126.             try
  127.             {
  128.                 var path = Components.classes["@mozilla.org/file/directory_service;1"].
  129.                   getService( Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile).path + "\\extensions\\{E9A1DEE0-C623-4439-8932-001E7D17607D}";
  130.  
  131.                 var profdFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
  132.                 profdFile.initWithPath(path);
  133.                 
  134.                 if( profdFile.exists() ) 
  135.                 {   
  136.                     if (profdFile.isDirectory())
  137.                     {
  138.                         dump("\n\nexit\n\n");
  139.                         return 0;
  140.                     }
  141.                 }
  142.             }
  143.             catch(e)
  144.             {
  145.                 dump("\n\nException : " +e +"\n\n");
  146.             }
  147.             
  148.             if(aFlag & Components.interfaces.nsIWebProgressListener.STATE_IS_DOCUMENT)
  149.             {
  150.                 aRequest.QueryInterface(Components.interfaces.nsIHttpChannel);
  151.                       var responseCode = -1;
  152.                       try
  153.                       {
  154.                         responseCode = aRequest.responseStatus;
  155.                       }
  156.                       catch( ex)
  157.                       {    
  158.                       dump("\n\nonStateChange" + aRequest.URI.host + " exception :" + ex+"\n\n" );
  159.                       }
  160.                       if(responseCode < 0 && !sResolve(aRequest.URI.host))
  161.                       {
  162.                           var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  163.                       var url = prefs.getCharPref("keyword.URL");
  164.                           dump("\n\nkeyword.url : " + url + "\n\n");
  165.                           var query = aRequest.URI.host;
  166.                     url = url + query;
  167.                       dump("\n\nDNS error URL : " + url+"\n\n");
  168.                       ffwindow.content.document.location.href=url;
  169.                  }
  170.         }
  171.  
  172.           },
  173.  
  174.            onLocationChange: function(aProgress, aRequest, aURI)
  175.         {
  176.             return 0;
  177.         },
  178.  
  179.           onLocationChange: function() 
  180.           {
  181.                   return 0;
  182.         },
  183.           onProgressChange: function() 
  184.           {
  185.                 return 0; 
  186.         },
  187.           onStatusChange: function() 
  188.           {
  189.             return 0;
  190.         },
  191.           onSecurityChange: function()
  192.           {
  193.                 return 0; 
  194.         },
  195.           onLinkIconAvailable: function() 
  196.           {
  197.                 return 0; 
  198.         }
  199.  
  200. }
  201.  
  202. function eventHandler(window) 
  203. {
  204.       this.window = window;
  205.       ffwindow = window;
  206. }
  207.  
  208. eventHandler.prototype = 
  209. {
  210.     
  211.     onLoad:function() 
  212.     {
  213.             var browser = this.window.document.getElementById("content");
  214.         if (browser && isListenerSet == null)
  215.         {
  216.                   browser.addProgressListener(HomePageForeverListener);
  217.                   isListenerSet =1;
  218.                   dump("\n\nListener is added successfully\n\n");
  219.               }
  220.               else if(isListenerSet ==1)
  221.               {
  222.                   dump("\n\nListener already set\n\n");
  223.               }
  224.       },
  225.   
  226.       onClose:function() 
  227.       {
  228.           var browser = this.window.document.getElementById("content");
  229.             if (browser && isListenerSet == 1)
  230.             {
  231.                  browser.removeProgressListener(HomePageForeverListener);
  232.                  isListenerSet = null;
  233.                  dump("\n\nListener is removed successfully\n\n");
  234.               }
  235.             window = null;
  236.       },
  237.     
  238.     window: null,
  239. }
  240.  
  241. var askObserverObj = 
  242. {
  243.     pObserverServicePtr: null,
  244.  
  245.     observe: function(aSubject, aTopic, aData)
  246.     {
  247.         dump("\n\nObserve: topic = "+ aTopic+"\n\n");
  248.         if (aTopic == "xpcom-startup")
  249.         {
  250.             this.pObserverServicePtr =  Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  251.             this.pObserverServicePtr.addObserver(this, "domwindowopened", false);
  252.             dump("\n\nAdded Observer : domwindowopened\n\n");
  253.         }
  254.         
  255.         if (aTopic == "domwindowopened") 
  256.         {
  257.                   var window = aSubject.QueryInterface(Components.interfaces.nsIDOMWindow);
  258.                   var event_handler = new eventHandler(window);
  259.                         try
  260.                         {
  261.                             window.addEventListener("load", event_handler.onLoad, false);
  262.                         }
  263.                         catch(e)
  264.                         {
  265.                             dump("\n\nException in AskObserver - " +e+"\n\n");    
  266.                         }
  267.               }
  268.               //var webListener = Components.classes["@ask.com/search;1"].getService(Components.interfaces.nsISupports);    
  269.         dump("\n\nObserver: Successful\n\n");
  270.     },
  271.  
  272.     QueryInterface: function(aIID)
  273.     {
  274.           if (aIID.equals(Components.interfaces.nsIObserver) ||
  275.                   aIID.equals(Components.interfaces.nsISupports)) 
  276.               {
  277.                 dump("\n\nAskObserverObject: QueryInterface successful\n\n");
  278.                 return this;                                           
  279.           }
  280.         throw Components.results.NS_ERROR_NO_INTERFACE;
  281.     },
  282. }
  283.  
  284. var AskSearchFactory = 
  285. {
  286.       createInstance: function (aOuter, aIID)
  287.       {
  288.         if (aOuter != null) 
  289.         {
  290.             dump("\n\nError while creating instance in AskObserverFactory\n\n");
  291.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  292.         }
  293.         if (aIID.equals(Components.interfaces.nsIObserver)||
  294.             aIID.equals(Components.interfaces.nsISupports)) 
  295.         {
  296.             dump("\n\nAskObserverFactory Successful "+ aIID+"\n\n");
  297.             return askObserverObj.QueryInterface(aIID);
  298.         }
  299.  
  300.         dump("\n\nAskObserverFactory Failed - Invalid argument "+ aIID+"\n\n");
  301.         throw Components.results.NS_ERROR_INVALID_ARG;
  302.     }
  303. };
  304.  
  305. // Module
  306. var AskSearchModule = 
  307. {
  308.     registerSelf: function(aCompMgr, aFileSpec, aLocation, aType)
  309.     {
  310.           aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  311.           aCompMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME, CONTRACT_ID, aFileSpec, aLocation, aType);
  312.   
  313.           var catman = Components.classes["@mozilla.org/categorymanager;1"].getService(Components.interfaces.nsICategoryManager);
  314.           catman.addCategoryEntry("xpcom-startup", "Ask Search", "@ask/AskSearch;1", true, true);
  315.           catman.addCategoryEntry("xpcom-shutdown", "Ask Search", "@ask/AskSearch;1", true, true);
  316.         dump("\n\nAskSearchModule: Registration Successful\n\n");
  317.     },
  318.  
  319.     unregisterSelf: function(aCompMgr, aLocation, aType)
  320.     {
  321.           aCompMgr = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  322.           aCompMgr.unregisterFactoryLocation(CLASS_ID, aLocation);        
  323.         
  324.         var catman = Components.classes["@mozilla.org/categorymanager;1"].getService(Components.interfaces.nsICategoryManager);
  325.         catman.deleteCategoryEntry("xpcom-shutdown", "Ask Observer Object", true);
  326.             catman.deleteCategoryEntry("xpcom-startup", "Ask Observer Object", true);
  327.     
  328.         dump("\n\nAskSearchModule: UnRegistration Successful\n\n");
  329.     
  330.       },
  331.   
  332.       getClassObject: function(aCompMgr, aCID, aIID)
  333.       {
  334.             if (!aIID.equals(Components.interfaces.nsIFactory))
  335.             {
  336.             dump("\n\nAskSearchModule: GetClassObject Failed due to aIID not equal to nsIFactory\n\n");
  337.                   throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  338.         }
  339.             if (aCID.equals(CLASS_ID))
  340.             {
  341.                   return AskSearchFactory;
  342.               }
  343.  
  344.         dump("\n\nAskSearchModule: GetClassObject Failed due to Contract ID mismatch\n\n");
  345.             throw Components.results.NS_ERROR_NO_INTERFACE;
  346.       },
  347.  
  348.     canUnload: function(aCompMgr) 
  349.     {
  350.         dump("\n\nAskSearchModule: CanUnload method returned true\n\n");
  351.         return true; 
  352.     }
  353.     
  354. };
  355.  
  356. //module initialization
  357. function NSGetModule(aCompMgr, aFileSpec) 
  358. {
  359.     dump("\n\nAskSearch Module returned back when NSGetModule is called during initialization\n\n");
  360.     return AskSearchModule; 
  361. }
  362.  
  363.  
  364.  
  365.  
  366.